//
//  TLAleartView.swift
//  TAleartView
//
//  Created by 唐基林 on 2025/1/15.
//

import UIKit
import SnapKit

public enum TAleartViewStyle {
    case TAleartViewStyleCenterTwo
    case TAleartViewStyleCenterOne
}

class TLAleartView: UIView {
    
    var dismissHandler: (() -> Void)?
    var okBtnClickHandler: ((String) -> Void)?

    /// 标题
     var titel: String
     
     /// 提示内容
     var content: String
     
     /// 取消
     var cancel: String
     
     /// 确定
     var okStr: String
    
    
    init(frame: CGRect, titel: String, content: String, cancel: String, okStr: String, style:TAleartViewStyle = .TAleartViewStyleCenterTwo) {
           self.titel = titel
           self.content = content
           self.cancel = cancel
           self.okStr = okStr
           super.init(frame: frame)
           self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        if style == .TAleartViewStyleCenterTwo {
            self .addSubview(self.centerView);
            self.centerView.snp.makeConstraints { make in
                make.left.bottom.top.right.equalTo(self)
            }
            self.centerView.okButtonClickHandler = { [weak self] str in
                self?.okBtnClickHandler?(str)
                self?.removeView()
            }
            self.centerView.dismissHandler = { [weak self] in
                self?.removeView()
            }
        } else if style == .TAleartViewStyleCenterOne {
            self.addSubview(self.centerViewOne)
            self.centerViewOne.snp.makeConstraints { make in
                make.left.bottom.top.right.equalTo(self)
            }
            self.centerViewOne.okButtonClickHandler = { [weak self] str in
                self?.okBtnClickHandler?(str)
                self?.removeView()
            }
        }
    }
       
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var centerView: TLAleartViewCenterTwo = {
        let view = TLAleartViewCenterTwo(frame: self.frame, titel: self.titel, content: self.content, cancel: self.cancel, okStr: self.okStr)
        return view;
    }()
    
    lazy var centerViewOne:TLAleartViewCenterOne = {
        let view = TLAleartViewCenterOne(frame: self.frame, titel: self.titel, content: self.content, okStr: self.okStr)
        return view
    }()
    
    private func removeView() {
        UIView.animate(withDuration: 0.3) {
            self.alpha = 0.0
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
}

